<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/11/27
 * Time: 14:19
 */

namespace app\admin\controller;


use app\Model\AdminModel;
use app\Model\AdminRoleModel;
use app\Model\FlagshipModel;
use app\Model\FlagshipSetModel;
use app\Model\OrderModel;
use app\Model\RegionModel;
use think\Collection;
//旗舰（分店）管理
class Flagship extends Common
{

    /*
     * 旗舰店账号页面
     */
    public function index()
    {
//        $this->_initialize();
        $postData = input('get.');
        $where = [];
        //筛选
        if (isset($postData['mobile'])){
            if ($postData['mobile']!=''){//收货人
                $where['mobile'] = ['LIKE', '%'.$postData['mobile'].'%'];
            }

        }
        $getFlag =FlagshipModel::
        where($where)->
        paginate(null,false,['query'=>request()->param()]);

//        dump($getFlag->toArray());die();
        $data['data'] = $getFlag;
        $this->getConAndAct('flagship');

        return view('index',$data );
    }
    /*
     * 设置页面
     */
    public function settingview()
    {
        $data['data']=$getFlag=FlagshipSetModel::column('*','id');

//        dump($getFlag);die();
        return view('settingview',$data);
    }
    /*
     * 设置
     */
    public function setting()
    {
        $getData=input('post.');
        $updata=[];
        foreach ($getData['setting'] as $k=>$v){
            $updata2=['id'=>$k,'val'=>$v];
            array_push($updata,$updata2);
        }
        $Flag=new FlagshipSetModel;
        $getDatabase=$Flag->saveAll($updata);
        return setDatabase($getDatabase);
    }
    /*
     * 新增/编辑管理员页面
     */
    public function add()
    {


        $getData = input('get.');
        $getRegion=RegionModel::where('level','=',1)->column('name','id');
        $data['province']=$getRegion;//省数据传入view
        if ($getData){
            $getAdmin = FlagshipModel::find($getData['id']);//获取信息
            $data['city']=RegionModel::where('parent_id',$getAdmin->province)->column('name','id');//市数据
            $data['district']=RegionModel::where('parent_id',$getAdmin->city)->column('name','id');//县、区数据
            $data['data']=$getAdmin;
//            dump($getAdmin->toArray());die();
        }
        return view('add',$data);//页面
    }
    /*
     * 新增/编辑
     */
    public function addData()
    {
        $postData = input('post.');
        if (isset($postData['id'])){
            sole2('tp_flagship',['mobile'=>$postData['mobile']],['id',$postData['id']]);//判断修改账号是否存在
            if ($postData['password2']!=''){//编辑密码
                $postData['update_time']=time();//最新修改时间
                $postData['password']=encryption($postData['password2']);//密码加密
            }
            unset($postData['password2']);
            $setAdmin = FlagshipModel::update($postData,['admin_id'=>$postData['id']]);//编辑
        }else{

            sole2('tp_flagship',['mobile'=>$postData['mobile']]);//判断账号是否存在
            $postData['password']=encryption($postData['password']);//密码加密
            $postData['create_time']=time();//加入时间

            $setAdmin = FlagshipModel::insert($postData);//新增
        }
        return setDatabase($setAdmin);
    }
    /*
     * 删除
     */
    public function del(){
        $postData = input('post.');


        $delRole = FlagshipModel::destroy($postData['id']);
        return setDatabase($delRole);
    }


    /*
     * 分店分布列表
     */
    public function districtindex()
    {

        $getProvince=RegionModel::where('parent_id',0)->column('name'); //获取所有省
        $getFlagRegion=FlagshipModel::select()->toArray();//获取所有分店账号地址

        if (!$getFlagRegion){
            $this->error('没有旗舰店账号');
        }
        $district=[];//分布
        foreach ($getProvince as $k=>$v){
            foreach ($getFlagRegion as $key=>$value){
                $where=['order_push'=>['<>',0],
                    'provinceName'=>$v,
                    'shipping_status'=>0];
                if ($value['provinceName']==$v){
                    //查询该店的订单
                    $where['order_push']=$value['id'];
                    $order=OrderModel::where($where)->count();
                    $value['order_num']=$order;

                    if (isset($district[$value['provinceName']])){
                        array_push($district[$value['provinceName']]['data'],$value);
                    }else{
                        $district[$value['provinceName']]['data'][0]=$value;
                    }
                    unset($getFlagRegion[$key]);
                }
            }
            if (isset($district[$v])){

                $order2= OrderModel::where($where)->count();//该省所有未发货推送订单
                $where['order_push']=-1;
                $order3= OrderModel::where($where)->count();//该省所有待抢单推送订单
                $district[$v]['order_num']['all']=$order2;
                $district[$v]['order_num']['comm']=$order3;
            }
        }
        $data['data']=$district;
//        dump($district);die();
        $this->getConAndAct('flagship');
        return view('districtindex',$data);

    }
}